<template>
  <div
    class="shopList display-flex flex-direction-column align-items-center justify-content-flex-center"
  >
    <div
      class="shopListItem display-flex flex-direction-row"
      v-for="(item,index) in list"
      @click="goDetail(item)"
      :key="index"
    >
      <div class="shopItemLeft display-flex flex-direction-column">
        <img v-lazy="yasuo(item.defaultPhoto)" alt style="width: 1.84rem;height: 1.84rem" />
        <div v-if="noStorage(item)" class="noStorage">
          <span>补货中</span>
        </div>
        <ul v-show="item.activityGoodsList" class="display-flex flex-direction-row">
          <li
            v-for="(a,aIndex) in deWeight(item.activityGoodsList)"
            :key="aIndex"
          >{{a.type==1?'特价':'搭赠'}}</li>
        </ul>
      </div>
      <div class="shopItemRight display-flex flex-direction-column">
        <p class="goodsName display-flex flex-direction-column justify-content-flex-center">
          <span>[{{item.brandName}}]{{(item.name&&item.name!=null)?item.name:''}}</span>
        </p>
        <div class="shopItemPackStock indent display-flex flex-direction-row">
          <p
            class="goodsDetail display-flex flex-direction-column align-items-center justify-content-flex-center"
          >
            <span>{{item.spec}}</span>
          </p>
          <p
            v-if="item.detail&&tabNum!=3"
            class="goodsDetailInfo display-flex flex-direction-column align-items-center justify-content-flex-center"
          >
            <span>{{item.detail}}</span>
          </p>
          <!--<p>库存:{{item.goodsStock>0?item.goodsStock:0}}{{item.baseUnitName}}</p>-->
        </div>
        <p
          v-if="item.detail&&tabNum==3"
          class="goodsDetailInfoTab3 display-flex flex-direction-column align-items-center justify-content-flex-center"
        >
          <span>{{item.detail}}</span>
        </p>
        <div
          v-for="(j,childJIndex) in item.unitVOList"
          :key="childJIndex"
          class="packPriceAndShopping display-flex flex-direction-column justify-content-flex-center"
        >
          <div class="indent display-flex flex-direction-row justify-content-flex-justify">
            <div
              v-if="j.unitGoodsPricesVO&&item.enquiry==0"
              class="goodsPackPrice display-flex flex-direction-row align-items-baseline"
            >
              <span>￥</span>
              <p>{{j.unitGoodsPricesVO?(j.unitGoodsPricesVO.discountTaxPrice?j.unitGoodsPricesVO.discountTaxPrice:j.unitGoodsPricesVO.taxPrice):'询价'}}</p>
              <span style="color: #999999;">/{{j.name}}</span>
            </div>
            <div v-if="item.enquiry!=0 || !j.unitGoodsPricesVO" class="display-flex flex-direction-row">
              <p style="color: #666666">询价</p>
              <span style="color: #999999;">&nbsp;/{{j.name}}</span>
            </div>
            <div class="fav" @click.stop="collect(index)" v-if="childJIndex == 0">
              <img
                src="@/assets/img/order/icon-fav.png"
                v-if="item.collectType == 0"
                class="icon"
                alt
              />
              <img src="@/assets/img/order/icon-fav-active.png" v-else class="icon" alt />
            </div>
            <div class="operation display-flex flex-direction-row" @click.stop>
              <!--item.enquiry==0-->
              <img
                style="width: .46rem;height: .46rem;"
                @click="reduceFn(j,item)"
                v-show="j.shoppingCartVO&&j.shoppingCartVO.num>0"
                src="../assets/img/cart/-@2x.png"
                alt
              />
              <p
                v-show="j.shoppingCartVO&&j.shoppingCartVO.num>0"
                @click="inputCountFn(j,item)"
                class="display-flex flex-direction-row align-items-center justify-content-flex-center"
              >
                <span
                  :style="{color:((shopZeroSaleState==1&&j.shoppingCartVO.num>j.goodsStock)?'#C9333C':((item.zeroSaleFlag==1&&j.shoppingCartVO.num>j.goodsStock)?'#C9333C':'#000000'))}"
                >{{j.shoppingCartVO?j.shoppingCartVO.num:''}}</span>
              </p>
              <!-- 不可销 -->
              <img v-if="j.stockFlag" src="../assets/img/order/addSlices2x.png" alt />
              <!-- 可销 -->
              <img
                v-if="!j.stockFlag"
                style="width: .46rem;height: .46rem;"
                @click="addFn(j,item)"
                src="../assets/img/cart/+@2x.png"
                alt
              />
            </div>
          </div>
          <p
            v-if="j.unitGoodsPricesVO&&j.unitGoodsPricesVO.limitNum"
            style="font-size: 12px;color:#FFAB91"
          >
            优惠限{{j.unitGoodsPricesVO.limitNum}}{{j.unitGoodsPricesVO?j.unitGoodsPricesVO.unitName:''}},
            第{{j.unitGoodsPricesVO.limitNum+1}}{{j.unitGoodsPricesVO?j.unitGoodsPricesVO.unitName:''}}起
            ￥{{j.unitGoodsPricesVO.taxPrice}}/{{j.unitGoodsPricesVO?j.unitGoodsPricesVO.unitName:''}}
          </p>
        </div>
      </div>
    </div>
  </div>
</template>
<script type="text/ecmascript-6">
export default {
  props: {
    list: Array,
    shopZeroSaleState: Number,
    tabNum: Number
  },
  watch: {
    list: function(newVal, oldVal) {
      this.list = newVal;
    }
  },
  computed: {
    noStorage() {
      return function(goods) {
        if (this.shopZeroSaleState == 0) return false;
        return goods.unitVOList
          ? goods.unitVOList.every(
              item => item.goodsStock <= 0 && item.zeroSaleFlag != 0
            )
          : true;
      };
    }
  },
  methods: {
    // 收藏
    collect(index) {
      var list = this.list;
      var goodsId = list[index]["id"];
      var collectType = list[index]["collectType"];
      var api = "replenish/collect/addCollect"; // 默认收藏
      if (collectType == 1) {
        api = "replenish/collect/delete";
      }
      this.$ajax.post(api, { goodsId }, res => {
        console.log(res);
        if (collectType == 0) {
          list[index]["collectType"] = 1;
        } else {
          list[index]["collectType"] = 0;
        }
      });
    },
    yasuo(img) {
      return img.indexOf("?") == -1 ? img + "?yasuo" : img;
    },
    deWeight(arr) {
      //对象数组去重
      if (arr == "" || arr == null) {
        arr = [];
      }
      const res = new Map();
      return arr.filter(a => !res.has(a.type) && res.set(a.type, 1));
    },
    goDetail(item) {
      //this.$vux.toast.text(this.$fromApp(this), 'center');
      if (this.$fromApp(this)) {
        // this.$callJava({url:this.$webUrl+"good/operate?from=app&goodId="+item.id+"&id="+this.$route.query.id})
        this.$router.push({
          path: "/good/operate",
          query: { goodId: item.id, from: "app", id: this.$route.query.id }
        });
      } else {
        this.$router.push({
          path: "/good/operate",
          query: { goodId: item.id, id: this.$route.query.id }
        });
      }
    },
    inputCountFn(val, item) {
      this.$vux.confirm.show({
        title: "请输入商品数量",
        value: true,
        showInput: true,
        inputAttrs: { type: "number" },
        onCancel: () => {},
        onConfirm: e => {
          if (this.$checkIsNumber(e)) {
            if (e.length > 8) {
              this.$vux.toast.text("最多输入8位", "center");
              return;
            }
            this.$ajax.post(
              "replenish/shopping_cart/setting",
              { goodsId: item.id, unitId: val.id, num: e },
              res => {
                if (res.code == 200) {
                  this.$root.eventHub.$emit("updateCart");
                  val.shoppingCartVO.num = e;

                  if (this.shopZeroSaleState == 0 && item.zeroSaleFlag == 0) {
                    val.stockFlag = false;
                  } else {
                    if (val.shoppingCartVO.num >= val.goodsStock) {
                      val.stockFlag = true;
                    } else {
                      val.stockFlag = false;
                    }
                  }
                } else {
                  this.$vux.toast.text(res.msg, "center");
                }
              }
            );
          } else {
            this.$vux.toast.text("请输入正整数", "center");
          }
        }
      });
    },
    addFn(val, item) {
      if (this.shopZeroSaleState == 0 && item.zeroSaleFlag == 0) {
        val.stockFlag = false;
      } else {
        if (val.shoppingCartVO.num >= val.goodsStock - 1) {
          val.stockFlag = true;
        } else {
          val.stockFlag = false;
        }
      }
      let count;
      if (val.shoppingCartVO.num != 0) {
        count = Number(val.shoppingCartVO.num) + 1;
        // val.shoppingCartVO.num = count;
      } else {
        count = 1;
      }
      let unitId;
      // if(item.enquiry!=0){
      //   unitId = val.unitGoodsPricesVO.unitId
      // }else{
      //   unitId = val.unitGoodsPricesVO.unitId;
      // }
      this.$ajax.post(
        "replenish/shopping_cart/setting",
        { goodsId: item.id, unitId: val.id, num: count },
        res => {
          // console.log(res);
          if (res.code == 200) {
            this.$root.eventHub.$emit("updateCart");
            val.shoppingCartVO.num = count;
          } else {
            this.$vux.toast.text(res.msg, "center");
          }
        }
      );
    },
    reduceFn(val, item) {
      if (this.shopZeroSaleState == 0 && item.zeroSaleFlag == 0) {
        val.stockFlag = false;
      } else {
        if (val.shoppingCartVO.num >= val.goodsStock + 1) {
          val.stockFlag = true;
        } else {
          val.stockFlag = false;
        }
      }
      let count = 0;
      if (val.shoppingCartVO.num != 0) {
        count = Number(val.shoppingCartVO.num) - 1;
      }
      this.$ajax.post(
        "replenish/shopping_cart/setting",
        { goodsId: item.id, unitId: val.id, num: count },
        res => {
          // console.log(res);
          if (res.code == 200) {
            this.$root.eventHub.$emit("updateCart");
            val.shoppingCartVO.num = count;
          } else {
            this.$vux.toast.text(res.msg, "center");
          }
        }
      );
    }
  }
};
</script>

<style lang="less" type="text/less" scoped>
/*弹性布局基本*/
.display-flex {
  display: -webkit-box;
  display: -ms-flexbox;
  display: -webkit-flex;
  display: flex;
}
.flex-direction-row {
  -webkit-box-orient: horizontal;
  -ms-flex-direction: row;
  -webkit-flex-direction: row;
  flex-direction: row;
}
.tab3 {
  height: 100%;
  .tab3L {
    width: 1.66rem;
    background-color: blue;
    p {
      height: 1.1rem !important;
      /*line-height: 1.1rem;*/
      font-size: 15px;
      .selectIcon {
        display: inline-block;
        width: 0.1rem;
        height: 0.3rem;
        margin-right: 0.15rem;
      }
      span {
        display: -webkit-box;
        -webkit-box-orient: vertical;
        -webkit-line-clamp: 2;
        overflow: hidden;
        width: 1.5rem;
      }
    }
  }
  .tab3R {
    width: 5.84rem;
    overflow-y: auto;
    .shopList {
      .shopListItem {
        width: 5.5rem;
        margin-left: -1.34rem;
        border-bottom: 1px solid #ebebeb;
        padding-bottom: 0.2rem;
        .shopItemLeft {
          width: 2rem;
          position: relative;
          img {
            width: 1.84rem;
            height: 1.84rem;
            margin-top: 0.25rem;
          }
          ul {
            position: absolute;
            top: 0.2rem;
            left: 0;
            li {
              margin-right: 0.1rem;
              color: #ffffff;
              font-size: 12px;
              background-color: #fc6e51;
              padding: 0.03rem 0.08rem;
            }
          }
        }
        .shopItemRight {
          width: 3.5rem;
          .goodsName {
            color: #333333;
            font-size: 14px;
            /*height: .8rem;*/
            margin: 0.2rem 0 0.1rem;
            /*line-height: .8rem;*/
            span {
              display: -webkit-box;
              -webkit-box-orient: vertical;
              -webkit-line-clamp: 2;
              overflow: hidden;
              width: 3.4rem;
            }
          }
          .goodsDetailInfoTab3 {
            color: #999999;
            width: 1.6rem;
            height: 0.3rem;
            line-height: 0.3rem;
            background-color: #ffffff;
            border-radius: 0.15rem;
            padding: 0 0.1rem;
            margin-top: 0.05rem;
            font-size: 10px;
            border: 1px solid #eeeeee;
          }
          .indent {
            height: 0.6rem;
            padding-right: 0.3rem;
            .goodsPackPrice {
              color: #fb6e52;
              span {
                font-size: 12px;
              }
              p {
                font-size: 18px;
              }
            }
            .goodsDetail {
              color: #999999;
              height: 0.32rem;
              line-height: 0.32rem;
              background-color: #eeeeee;
              border-radius: 0.16rem;
              padding: 0 0.1rem;
              margin-top: 0.05rem;
              font-size: 10px;
            }
            .goodsDetailInfo {
              color: #999999;
              height: 0.3rem;
              line-height: 0.3rem;
              background-color: #ffffff;
              border-radius: 0.15rem;
              padding: 0 0.1rem;
              margin-top: 0.05rem;
              font-size: 10px;
              margin-left: 0.15rem;
              border: 1px solid #eeeeee;
            }
            .operation {
              img {
                width: 0.46rem;
                height: 0.46rem;
              }
              p {
                margin: 0 0.1rem;
                width: 0.6rem;
                height: 0.5rem;
                border-radius: 2px;
                background-color: #eeeeee;
              }
            }
          }
          .packPriceAndShopping {
            margin-top: 0.1rem;
            border-bottom: 1px solid #ebebeb;
          }
          .packPriceAndShopping:last-child {
            border-bottom: none;
          }
        }
      }
    }
  }
}
.shopList {
  .shopListItem {
    width: 7.2rem;
    margin-left: 0.3rem;
    border-bottom: 1px solid #ebebeb;
    padding-bottom: 0.2rem;
    .shopItemLeft {
      width: 2.4rem;
      position: relative;
      img {
        width: 1.84rem;
        height: 1.84rem;
        margin-top: 0.25rem;
      }
      ul {
        position: absolute;
        top: 0.2rem;
        left: 0;
        li {
          margin-right: 0.1rem;
          color: #ffffff;
          font-size: 12px;
          background-color: #fc6e51;
          padding: 0.03rem 0.08rem;
        }
      }
    }
    .shopItemRight {
      width: 4.8rem;
      .goodsName {
        color: #333333;
        font-size: 15px;
        /*height: .8rem;*/
        margin: 0.2rem 0 0.1rem;
        /*line-height: .8rem;*/
        span {
          display: -webkit-box;
          -webkit-box-orient: vertical;
          -webkit-line-clamp: 2;
          overflow: hidden;
          width: 4.7rem;
        }
      }
      .indent {
        height: 0.6rem;
        padding-right: 0.3rem;
        .goodsPackPrice {
          color: #fb6e52;
          span {
            font-size: 12px;
          }
          p {
            font-size: 18px;
          }
        }
        .goodsDetail {
          color: #999999;
          height: 0.32rem;
          line-height: 0.32rem;
          background-color: #eeeeee;
          border-radius: 0.16rem;
          padding: 0 0.15rem;
          margin-top: 0.05rem;
          font-size: 10px;
        }
        .goodsDetailInfo {
          color: #999999;
          height: 0.3rem;
          line-height: 0.3rem;
          background-color: #ffffff;
          border-radius: 0.15rem;
          padding: 0 0.1rem;
          margin-top: 0.05rem;
          font-size: 10px;
          margin-left: 0.15rem;
          border: 1px solid #eeeeee;
        }
        .operation {
          img {
            width: 0.46rem;
            height: 0.46rem;
          }
          p {
            margin: 0 0.1rem;
            width: 0.6rem;
            height: 0.5rem;
            border-radius: 2px;
            background-color: #eeeeee;
          }
        }
      }
      .packPriceAndShopping {
        margin-top: 0.1rem;
        border-bottom: 1px solid #ebebeb;
      }
      .packPriceAndShopping:last-child {
        border-bottom: none;
      }
    }
  }
}
.noStorage {
  position: absolute;
  top: 0.25rem;
  left: 0;
  width: 1.84rem;
  height: 1.84rem;
  background-color: rgba(0, 0, 0, 0.4);
  color: white;
  // line-height: 1.84rem;
  // text-align: center;
  font-size: 0.32rem;
  display: flex;
  align-items: center;
  justify-content: center;
}
</style>
